package hero.mps.leet_code_13;

import java.awt.geom.CubicCurve2D;
import java.util.HashMap;
import java.util.Map;

public class RomanConvertInteger {
    public int romanToInt(String s) {
        Map<Character, Integer> romanMap = new HashMap<>();
        romanMap.put('I', 1);
        romanMap.put('V', 5);
        romanMap.put('X', 10);
        romanMap.put('L', 50);
        romanMap.put('C', 100);
        romanMap.put('D', 500);
        romanMap.put('M', 1000);
        int result = 0;
        int prevValue = 0;
        int curValue;
        for (int i = s.length() - 1; i >= 0; i--) {
            curValue = romanMap.get(s.charAt(i));
            if (curValue < prevValue) {
                result -= curValue;
            } else {
                result += curValue;
            }
            prevValue = curValue;
        }
        return result;
    }
    public int romanToInt1(String s) {
        int[] values = new int[128]; // ASCII 码的范围足够覆盖所有字符
        values['I'] = 1;
        values['V'] = 5;
        values['X'] = 10;
        values['L'] = 50;
        values['C'] = 100;
        values['D'] = 500;
        values['M'] = 1000;
        int result = 0;
        int prevValue = 0;
        int curValue;
        for (int i = s.length() - 1; i >= 0; i--) {
            curValue = values[s.charAt(i)];
            if (curValue < prevValue) {
                result -= curValue;
            } else {
                result += curValue;
            }
            prevValue = curValue;
        }
        return result;
    }


    public static void main(String[] args) {
        System.out.println(new RomanConvertInteger().romanToInt1("III"));
    }
}
